Skip to content

Read the API token from CONVERTAPI_TOKEN when none is set in code - #96

Merged
tomasr78 merged 2 commits into
mainfrom
token-from-environment
Nov 12, 2026
Merged

tomasr78 merged 2 commits into
mainfrom
token-from-environment

Conversation

@tomasr78

Copy link
Copy Markdown
Contributor

Part of 3.3.0. When no token is set in code, the SDK now reads it from the CONVERTAPI_TOKEN environment variable, the name the README and the examples already use.

What changed

  • Token lookup: a token set in code (new ConvertApi("...") or ConvertApi.ApiToken) always comes first. Otherwise each request reads CONVERTAPI_TOKEN, with surrounding whitespace trimmed. new ConvertApi() now works without a token in the source code.
  • Clear error before anything is sent: with no token in code and none in the variable, uploads, ConvertAsync and GetUserAsync throw a ConvertApiException with StatusCode 401. The message says how to set the token and links to the token page, tagged utm_source=dotnet-sdk&utm_content=error-no-token. Before this change, the SDK uploaded the input files first and then got a 401 from the API. The exception type and status code stay the same, so code that handled that 401 keeps working. Response holds the same text as Message.
  • new ConvertApi("") and new ConvertApi(null) still throw ArgumentNullException, and the message now names CONVERTAPI_TOKEN.
  • ConvertApi.TokenEnvironmentVariable is a new public constant holding the variable's name.
  • Requests that also work without a token (the OpenAPI lookup) send the token if there is one and nothing otherwise, as before.
  • README: the "Set the API token" section says that from 3.3.0 on, new ConvertApi() reads the variable by itself. The snippets keep Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"), because they must also work with 3.2.2, which is what nuget.org serves today.
  • CHANGELOG: entries under [Unreleased] / Added.

How it was tested

  • The CI steps, run locally: restore, Release build, dotnet test (9 of 9 pass: the 4 existing tests and 5 new ones in ConvertApi.Tests/TokenTests.cs), and pack. The build has the same 3 warnings as main. NU5048 (PackageIconUrl) was already there and is handled separately in 3.3.0.
  • The new unit tests use a fake IConvertApiHttpClient that records the bearer token of each request. They check that:
    • a token set in code wins over the variable;
    • with no token in code, the variable is used, trimmed, for the upload, the conversion and GetUserAsync;
    • with no token at all, a 401 ConvertApiException is thrown and no request is sent;
    • GetUserAsync with no token sends nothing;
    • new ConvertApi("") names the variable.
      The tests restore the static token, the static client and the variable afterwards.
  • Against the staging API, using a console app built on this branch:
    • new ConvertApi() with only CONVERTAPI_TOKEN set converted Examples/TestFiles/test.docx to a 146,734-byte PDF. GetUserAsync worked, and DeleteFilesAsync removed both server files.
    • With no token anywhere: a 401 ConvertApiException with the message above, and no upload.
    • With a wrong token in code and a valid variable: the API answered 401, which shows the token set in code was the one sent.

🤖 Generated with Claude Code

A token set in code (new ConvertApi("...") or ConvertApi.ApiToken) comes
first; otherwise each request reads the CONVERTAPI_TOKEN environment
variable. With neither, uploads, ConvertAsync and GetUserAsync throw a
ConvertApiException with status 401 that says how to set the token, before
anything is sent. Earlier versions uploaded the input files and then got
the API's 401.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 12:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The missing-token path throws before StreamContent takes ownership of the supplied FileStream, leaving it open and regressing upload-failure cleanup.

1 open issue
What changed in this PR

Adds environment-based token lookup to the SDK while preserving precedence for tokens set in code and existing constructor validation.

Changes:

  • Reads and trims CONVERTAPI_TOKEN when no token is set in code.
  • Rejects authenticated operations locally with a 401 ConvertApiException when no token is available.
  • Adds token-resolution tests and documents the behavior planned for 3.3.0.
File Description
README.md Documents automatic token lookup and explicit-constructor validation.
ConvertApi/​ConvertApiParam.cs Resolves tokens before stream and remote-file uploads.
ConvertApi/​ConvertApi.cs Centralizes token lookup and missing-token errors.
ConvertApi.Tests/​TokenTests.cs Tests token precedence, environment fallback, and missing-token behavior.
CHANGELOG.md Records token lookup and validation changes under Unreleased.

Review effort: Balanced

Comment thread ConvertApi/ConvertApiParam.cs Resolved
Resolve the token inside the StreamContent using, so the early missing-token
failure still disposes the caller's stream, as a failed upload always did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 12:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the environment-based authentication change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@tomasr78
tomasr78 merged commit 81eda5e into main Nov 12, 2026
4 checks passed
@tomasr78
tomasr78 deleted the token-from-environment branch November 12, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants